import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import router from '@/router'

axios.defaults.baseURL = '/api';
axios.defaults.timeout = 8000

// 接口拦截白名单
const whiteList = {
    "/user": "get",
    "/carts/products/sum": "get"
}

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    let res = response.data
    if(res.status === 0) {
        return res.data
    }
    else if(res.status === 10) {
        if(whiteList[response.config.url] === whiteList[response.config.method]) {
            router.push({
                path: '/login',
            })
        }
        return Promise.reject(res)
    } else {
        Vue.prototype.$message.error(res.msg)
        return Promise.reject(res)
    }

}, function (error) {
    return Promise.reject(error)
});
Vue.use(VueAxios, axios)
